<?php
//=======================================
//###################################
// Kayako Web Solutions
//
// Source Copyright 2001-2004 Kayako Web Solutions
// Unauthorized reproduction is not allowed
// License Number: $%LICENSE%$
// $Author: vshoor $ ($Date: 2006/05/26 17:32:08 $)
// $RCSfile: staff_chathistory.php,v $ : $Revision: 1.10 $ 
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
//###################################
//=======================================

if (!defined("INSWIFT")) {
	trigger_error("Unable to process $PHP_SELF", E_USER_ERROR);
}

if ($_SWIFT["staff"]["ls_entab"] == "0")
{
	echo '<font face="Verdana, Arial, Helvetica" size="2" color="red"><b>'.$_SWIFT["language"]["nopermission"].'</b></font>';
	exit;
}

require_once ("./modules/livesupport/functions_livesupport.php");
require_once ("./modules/livesupport/functions_chathistory.php");
require_once ("./includes/functions_html.php");
$template->loadLanguageSection("chathistory");

$grid = new Grid();

$interface->assignQuickLink($_SWIFT["language"]["chathistory"], "index.php?_m=livesupport&_a=chathistory", "icon_user.gif");

/**
* ###############################################
* FUNCTION DECLARATIONS
* ###############################################
*/


/**
* Callback for parsing Chat Object data
*/
function _chatObjectFields($arg)
{
	global $_SWIFT, $_sladays;
	$record = &$arg[0];
	$fields = &$arg[1];

	$record["chatobjects.useremail"] = htmlspecialchars($record["useremail"]);
	$record["chatobjects.staffid"] = htmlspecialchars($_SWIFT["staffcache"][$record["staffid"]]["fullname"]);
	$record["chatobjects.departmentid"] = htmlspecialchars($_SWIFT["departmentcache"][$record["departmentid"]]["title"]);

	$diff = iif(!empty($record["lastpostactivity"]), ($record["lastpostactivity"]-$record["dateline"]), 0);
	$duration = varDate($diff);
	$record["chatobjects.dateline"] = '<a href="javascript:void(0);" onMouseOver="return overlib(\''. $_SWIFT["language"]["duration"] .$duration.'\');" onMouseOut="return nd();">'.edate($_SWIFT["settings"]["dt_datetimeformat"], $record["dateline"]).'</a>';

	if ($record["chattype"] == CHAT_STAFF)
	{
		$statusicon = "icon_chatprivate.gif";
	} else {
		$statusicon = "icon_chat.gif";
	}
	
	$record["chatobjects.userfullname"] = '<table width="100%"  border="0" cellspacing="0" cellpadding="1"><tr><td width="1"><img src="'. $_SWIFT["themepath"] .$statusicon.'" border="0" /></td><td><span class="smalltext">&nbsp;<a href="index.php?_m=livesupport&_a=viewchat&chatobjectid='. $record["chatobjectid"] .'">'. htmlspecialchars($record["userfullname"]) .'</a></span></td></tr></table>';

	$record["options"] = '<a href="index.php?_m=livesupport&_a=viewchat&chatobjectid='. $record["chatobjectid"] .'" title="'. $_SWIFT["language"]["view"] .'"><img src="'. $_SWIFT["themepath"] .'icon_conversation.gif" border="0" align="absmiddle">&nbsp;'. $_SWIFT["language"]["view"] .'</a>&nbsp;&nbsp;&nbsp;&nbsp;<a onClick="javascript:doConfirm(\''. $_SWIFT["language"]["chatobjectdelconfirmmsg"] .'\', \'index.php?_m=livesupport&_a=chathistory&do=delete&chatobjectid='. $record["chatobjectid"] .'\');" href="#" title="'. $_SWIFT["language"]["delete"] .'"><img src="'. $_SWIFT["themepath"] .'icon_delete.gif" border="0" align="absmiddle">&nbsp;'. $_SWIFT["language"]["delete"] .'</a>&nbsp;';

	return $record;
}

/**
* Mass Action: Delete Chat Objects
*/
function _maDeleteChatObjects()
{
	global $dbCore, $_SWIFT, $infomessage;

	if (_is_array($_POST["itemid"]))
	{
		$infomessage = $_SWIFT["language"]["chatobjectdelconfirm"];
		deleteChatObjects($_POST["itemid"]);
	}
}

/**
* ###############################################
* CHAT HISTORY
* ###############################################
*/
if ($eventaction == "chathistory")
{
	if ($_GET["do"] == "delete" && trim($_GET["chatobjectid"]) != "")
	{
		$infomessage = $_SWIFT["language"]["chatobjectdelconfirm"];

		deleteChatObjects(array($_GET["chatobjectid"]));
	}

	unset($filtersql);

	$template->assign("calendar", true);
	$template->assign("overlib", true);
	$template->assign("lschathistory", true);
	if ($_GET["filter"] == 1)
	{
		$template->assign("calfilterdate", str_replace("-", "/", $_GET["filterdate"]));
		$dateobjects = explode("-", $_GET["filterdate"]);
		$year = $dateobjects[0];
		$month = $dateobjects[1];
		$day = $dateobjects[2];
		$start = mktime(0,0,0,$month,$day,$year);
		$end = mktime(23, 59, 59, $month, $day, $year);
		$filtersql = " AND `dateline` >= '". intval($start) ."' AND `dateline` <= '". intval($end) ."'";
	}
	$interface->staffHeader($_SWIFT["language"]["livesupport"]. " &gt; ".$_SWIFT["language"]["chathistory"], 3);

	$interface->staffNavBar('<a href="index.php?_m=livesupport&_a=chathistory" title="'.$_SWIFT["language"]["livesupport"].'">'.$_SWIFT["language"]["livesupport"].'</a> &raquo; <a href="index.php?_m=livesupport&_a=chathistory" title="'.$_SWIFT["language"]["chathistory"].'">'.$_SWIFT["language"]["chathistory"].'</a>', "", 3);

	$options["recordsperpage"] = "10";
	$options["sortby"] = "chatobjects.dateline";
	$options["sortorder"] = "desc";

	$options["massaction"][0]["title"] = $_SWIFT["language"]["delete"];
	$options["massaction"][0]["callback"] = "_maDeleteChatObjects";

	$options["idname"] = "chatobjectid";
	$options["quicksearch"] = true;

	$options["advancedsearch"][0]["title"] = $_SWIFT["language"]["chatid"];
	$options["advancedsearch"][0]["query"] = 'SELECT * FROM `'. TABLE_PREFIX .'chatobjects` AS chatobjects LEFT JOIN `'. TABLE_PREFIX .'chatdata` AS chatdata ON (chatobjects.chatobjectid = chatdata.chatobjectid) WHERE (chatobjects.departmentid IN ('. buildIN($departmentidlist) .')'.$filtersql.') AND (chatobjects.chatobjectid = \'%$_searchstr%\') $_sortjoin;';
	$options["advancedsearch"][1]["title"] = $_SWIFT["language"]["fullname"];
	$options["advancedsearch"][1]["query"] = 'SELECT * FROM `'. TABLE_PREFIX .'chatobjects` AS chatobjects LEFT JOIN `'. TABLE_PREFIX .'chatdata` AS chatdata ON (chatobjects.chatobjectid = chatdata.chatobjectid) WHERE (chatobjects.departmentid IN ('. buildIN($departmentidlist) .')'.$filtersql.') AND (chatobjects.userfullname LIKE \'%$_searchstr%\') $_sortjoin;';
	$options["advancedsearch"][2]["title"] = $_SWIFT["language"]["email"];
	$options["advancedsearch"][2]["query"] = 'SELECT * FROM `'. TABLE_PREFIX .'chatobjects` AS chatobjects LEFT JOIN `'. TABLE_PREFIX .'chatdata` AS chatdata ON (chatobjects.chatobjectid = chatdata.chatobjectid) WHERE (chatobjects.departmentid IN ('. buildIN($departmentidlist) .')'.$filtersql.') AND (chatobjects.useremail LIKE \'%$_searchstr%\') $_sortjoin;';
	$options["advancedsearch"][3]["title"] = $_SWIFT["language"]["contents"];
	$options["advancedsearch"][3]["query"] = 'SELECT * FROM `'. TABLE_PREFIX .'chatobjects` AS chatobjects LEFT JOIN `'. TABLE_PREFIX .'chatdata` AS chatdata ON (chatobjects.chatobjectid = chatdata.chatobjectid) WHERE (chatobjects.departmentid IN ('. buildIN($departmentidlist) .')'.$filtersql.') AND (chatdata.contents LIKE \'%$_searchstr%\') $_sortjoin;';

	$options["callback"] = "_chatObjectFields";

	$fields[0]["name"] = "chatobjects.userfullname";
	$fields[0]["title"] = $_SWIFT["language"]["fullname"];
	$fields[0]["width"] = "";

	$fields[1]["name"] = "chatobjects.staffid";
	$fields[1]["title"] = $_SWIFT["language"]["staffname"];
	$fields[1]["width"] = "140";
	$fields[1]["align"] = "center";

	$fields[2]["name"] = "chatobjects.departmentid";
	$fields[2]["title"] = $_SWIFT["language"]["chatdepartment"];
	$fields[2]["width"] = "140";
	$fields[2]["align"] = "center";

	$fields[3]["name"] = "chatobjects.dateline";
	$fields[3]["title"] = $_SWIFT["language"]["date"];
	$fields[3]["width"] = "140";
	$fields[3]["align"] = "center";

	$departmentidlist = getChatDepartments();

	$grid->start("chat", $_SWIFT["language"]["chatlist"], 'SELECT * FROM `'. TABLE_PREFIX .'chatobjects` AS chatobjects WHERE chatobjects.departmentid IN ('. buildIN($departmentidlist) .')'.$filtersql.' $_sortjoin;', 'SELECT COUNT(*) AS totalitems FROM `'. TABLE_PREFIX .'chatobjects`AS chatobjects WHERE chatobjects.departmentid IN ('. buildIN($departmentidlist) .')'.$filtersql.';', 'SELECT * FROM `'. TABLE_PREFIX .'chatobjects` AS chatobjects LEFT JOIN `'. TABLE_PREFIX .'chatdata` AS chatdata ON (chatobjects.chatobjectid = chatdata.chatobjectid) WHERE (chatobjects.departmentid IN ('. buildIN($departmentidlist) .')'.$filtersql.') AND (chatobjects.chatobjectid = \'$_searchstr\' OR chatobjects.userfullname LIKE \'%$_searchstr%\' OR chatobjects.useremail LIKE \'%$_searchstr%\' OR chatdata.contents LIKE \'%$_searchstr%\') $_sortjoin;', $fields, $options);

	printInfoBox($infomessage);
	printErrorBox($errormessage);

	$grid->display("chat");

	echo '<BR /><span class="smalltext">'. $_SWIFT["language"]["legend"] .' <img src="'. $_SWIFT["themepath"] .'icon_chat.gif" border="0" /> '. $_SWIFT["language"]["userchat"].'&nbsp;&nbsp;&nbsp; <img src="'. $_SWIFT["themepath"] .'icon_chatprivate.gif" border="0" /> '. $_SWIFT["language"]["staffchat"].'</span>';

	$interface->staffFooter();








/**
* ###############################################
* VIEW CHAT
* ###############################################
*/
} else if ($eventaction == "viewchat") {
	$departmentidlist = getChatDepartments();
	// Get the chat object
	$_chatobject = $dbCore->queryFetch("SELECT * FROM `". TABLE_PREFIX ."chatobjects` AS chatobjects LEFT JOIN `". TABLE_PREFIX ."chatdata` AS chatdata ON (chatobjects.chatobjectid = chatdata.chatobjectid) WHERE chatobjects.chatobjectid = '". intval($_REQUEST["chatobjectid"]) ."';");
	if (empty($_chatobject["chatobjectid"]) || (!in_array($_chatobject["departmentid"], $departmentidlist) && $_chatobject["chattype"] != CHAT_STAFF))
	{
		trigger_error($_SWIFT["language"]["invalidchatobject"], E_USER_ERROR);
	}

	$template->assign("calendar", true);
	$template->assign("overlib", true);
	$template->assign("lschathistory", true);

	$template->assign("itemoptiontitle", $_SWIFT["language"]["chatoptions"]);
	$interface->assignItemOption($_SWIFT["language"]["deletechat"], "", "icon_delete.gif", "", 'doConfirm(\''. $_SWIFT["language"]["chatobjectdelconfirmmsg"] .'\', \'index.php?_m=livesupport&_a=chathistory&do=delete&chatobjectid='. $_chatobject["chatobjectid"] .'\')');

	$interface->staffHeader($_SWIFT["language"]["livesupport"]. " &gt; ".$_SWIFT["language"]["chathistory"], 3);

	$interface->staffNavBar('<a href="index.php?_m=livesupport&_a=chathistory" title="'.$_SWIFT["language"]["chathistory"].'">'.$_SWIFT["language"]["chathistory"].'</a> &raquo; <a href="index.php?_m=livesupport&_a=viewchat&chatobjectid='. intval($_chatobject['chatobjectid']).'" title="'.$_SWIFT["language"]["viewchat"].'">'.sprintf($_SWIFT["language"]["viewchatd"], $_chatobject["chatobjectid"]).'</a>', "", 3);

	printInfoBox($infomessage);
	printErrorBox($errormessage);

	printMainTableHeader(sprintf($_SWIFT["language"]["chatidd"], $_chatobject["chatobjectid"]));

	$diff = iif(!empty($_chatobject["lastpostactivity"]), ($_chatobject["lastpostactivity"]-$_chatobject["dateline"]), 0);
	$duration = varDate($diff);

	$infolist = $colarray = array();
	$infolist[] = array($_SWIFT["language"]["chfullname"], htmlspecialchars($_chatobject["userfullname"]));
	$infolist[] = array($_SWIFT["language"]["chemail"], htmlspecialchars($_chatobject["useremail"]));
	$infolist[] = array($_SWIFT["language"]["chdate"], edate($_SWIFT["settings"]["dt_datetimeformat"], $_chatobject["dateline"]));
	$infolist[] = array($_SWIFT["language"]["chduration"], $duration);
	$infolist[] = array($_SWIFT["language"]["chstaff"], htmlspecialchars($_SWIFT["staffcache"][$_chatobject["staffid"]]["fullname"]));
	$infolist[] = array($_SWIFT["language"]["chdepartment"], htmlspecialchars($_SWIFT["departmentcache"][$_chatobject["departmentid"]]["title"]));
	$infolist[] = array($_SWIFT["language"]["chtransferstatus"], iif($_chatobject["transferstatus"]==TRANSFER_ACCEPTED, $_SWIFT["language"]["yes"], $_SWIFT["language"]["no"]));
	$infolist[] = array($_SWIFT["language"]["chtransferfrom"], htmlspecialchars($_SWIFT["staffcache"][$_chatobject["transferfromid"]]["fullname"]));
	$infolist[] = array($_SWIFT["language"]["chipaddress"], htmlspecialchars($_chatobject["ipaddress"]));
	$infolist[] = array($_SWIFT["language"]["chchattype"], '<img src="'.$_SWIFT["themepath"].iif($_chatobject["chattype"]==CHAT_STAFF, "icon_staff.gif", "icon_user.gif").'" /> '.iif($_chatobject["chattype"]==CHAT_STAFF, $_SWIFT["language"]["staffchat"], $_SWIFT["language"]["userchat"]));

	printDescRow($_SWIFT["language"]["chatinformation"], 4);
	$cnt = $index = 0;
	$lastindex = count($infolist)-1;
	for ($ii=0; $ii<count($infolist); $ii++)
	{
		$colarray[$index]["align"] = "left";
		$colarray[$index]["valign"] = "top";
		$colarray[$index]["nowrap"] = true;
		$colarray[$index]["value"] = $infolist[$ii][0];
		$colarray[$index]["class"] = "row1";
		$index++;
		$colarray[$index]["align"] = "left";
		$colarray[$index]["valign"] = "top";
		$colarray[$index]["nowrap"] = true;
		$colarray[$index]["value"] = $infolist[$ii][1];
		$colarray[$index]["class"] = "row2";
		$cnt++; $index++;

		if ($cnt == 2 || $ii == $lastindex)
		{
			printDataRow($colarray);
			$colarray = array();
			$index = 0;
			$cnt=0;
		}
	}

	$chatcontents = unserialize($_chatobject["contents"]);
	unset($_chat);
	$_chat = '<span class="mediumtext">';
	foreach ($chatcontents as $key=>$val)
	{
		$_chat .= '<span class="'. iif($val["type"]=="staff", "staffname", "clientname") .'">'. htmlspecialchars($val["name"]) .'</span>: <span class="'. iif($val["type"]=="staff", "staffmessage", "clientmessage") .'">'. str_replace("&amp;#", "&#", htmlspecialchars(iif($val["base64"]==true, base64_decode($val["message"]), $val["message"]))) .'</span><BR />';
	}
	$_chat .= '</span>';
	
	printDescRow($_SWIFT["language"]["chconversation"], 4);
	$colarray = array();
	$colarray[0]["align"] = "left";
	$colarray[0]["valign"] = "top";
	$colarray[0]["colspan"] = "4";
	$colarray[0]["nowrap"] = true;
	$colarray[0]["value"] = '<div style="position:relative; width:100%; height:300px; background-color:#ffffff; overflow:auto;">'.$_chat.'</div>';
	$colarray[0]["class"] = "row1";
	printDataRow($colarray);


	printMainTableFooter();


	$template->assign("backurl", "index.php?_m=livesupport&_a=chathistory");
	$interface->staffFooter();
}

?>